package com.rcloud.stock.database;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class Repository {
    private final HistoryDao historyDao;
    public Repository(Context context) {
        Database database = Database.getDatabase(context.getApplicationContext());
        historyDao = database.getHistoryDao();
    }

    public LiveData<List<History>> getHistories(){
        return historyDao.getHistories();
    }

    public void insertHistories(History... histories) {
        new InsertAsyncTask(historyDao).execute(histories);
    }

    public void updateHistories(History... histories) {
        new UpdateAsyncTask(historyDao).execute(histories);
    }

    public void deleteHistories(History... histories) {
        new DeleteAsyncTask(historyDao).execute(histories);
    }
    
    static class InsertAsyncTask extends AsyncTask<History, Void, Void> {
        private final HistoryDao historyDao;

        InsertAsyncTask(HistoryDao historyDao) {
            this.historyDao = historyDao;
        }

        @Override
        protected Void doInBackground(History... histories) {
            historyDao.insertHistories(histories);
            return null;
        }
    }
    static class UpdateAsyncTask extends AsyncTask<History, Void, Void> {
        private final HistoryDao historyDao;

        UpdateAsyncTask(HistoryDao historyDao) {
            this.historyDao = historyDao;
        }

        @Override
        protected Void doInBackground(History... histories) {
            historyDao.updateHistories(histories);
            return null;
        }

    }
    static class DeleteAsyncTask extends AsyncTask<History, Void, Void> {
        private final HistoryDao historyDao;

        DeleteAsyncTask(HistoryDao historyDao) {
            this.historyDao = historyDao;
        }

        @Override
        protected Void doInBackground(History... histories) {
            historyDao.deleteHistories(histories);
            return null;
        }
    }
}

